/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.wdb;

import org.codehaus.groovy.runtime.ArrayUtil;

import java.lang.management.*;
import java.net.UnknownHostException;
import java.util.List;
import java.util.TimeZone;

/**
 */
public class Test {
    public static void main(String[] args) throws UnknownHostException {
        RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
        String vmName = runtimeMXBean.getVmName();
        String vmVersion = runtimeMXBean.getVmVersion();
        String vmVendor = runtimeMXBean.getVmVendor();
        String name = runtimeMXBean.getName();
        long uptime = runtimeMXBean.getUptime();

        System.out.println(System.getProperty("java.version"));
        System.out.println("<tr><th>虚拟机</th><td>" + vmName + " 版本" + vmVersion + "</td></tr>");
        System.out.println("<tr><th>供应商</th><td>" + vmVendor + "</td></tr>");
        System.out.println("<tr><th>名称</th><td>" + name + "</td></tr>");
        System.out.println("<tr><th>运行时间</th><td>" + uptime / 1000 / 60 / 60 + "分" + "</td></tr>");

        CompilationMXBean compilationMXBean = ManagementFactory.getCompilationMXBean();
        String jitName = compilationMXBean.getName();
        long totalCompilationTime = compilationMXBean.getTotalCompilationTime();

        System.out.println("<tr><th>即时编译器</th><td>" + jitName + "</td></tr>");
        System.out.println("<tr><th>编译总时间</th><td>" + totalCompilationTime / 1000 + "秒" + "</td></tr>");

        // --------------------------------------------
        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        int threadCount = threadMXBean.getThreadCount();
        int peakThreadCount = threadMXBean.getPeakThreadCount();
        int daemonThreadCount = threadMXBean.getDaemonThreadCount();
        long totalStartedThreadCount = threadMXBean.getTotalStartedThreadCount();
        System.out.println("<tr><th>活动线程</th><td>" + threadCount + "</td></tr>");
        System.out.println("<tr><th>线程峰值</th><td>" + peakThreadCount + "</td></tr>");
        System.out.println("<tr><th>守护线程</th><td>" + daemonThreadCount + "</td></tr>");
        System.out.println("<tr><th>已启动线程总数</th><td>" + totalStartedThreadCount + "</td></tr>");

        ClassLoadingMXBean classLoadingMXBean = ManagementFactory.getClassLoadingMXBean();
        int loadedClassCount = classLoadingMXBean.getLoadedClassCount();
        long unloadedClassCount = classLoadingMXBean.getUnloadedClassCount();
        long totalLoadedClassCount = classLoadingMXBean.getTotalLoadedClassCount();
        System.out.println("<tr><th>当前类已装入</th><td>" + loadedClassCount + "</td></tr>");
        System.out.println("<tr><th>已经装入类总数</th><td>" + totalLoadedClassCount + "</td></tr>");
        System.out.println("<tr><th>已经卸载类总数</th><td>" + unloadedClassCount + "</td></tr>");

        // ------------------------------------
        MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
        MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
        long init = heapMemoryUsage.getInit();
        long used = heapMemoryUsage.getUsed();
        long committed = heapMemoryUsage.getCommitted();
        long max = heapMemoryUsage.getMax();
        System.out.println("<tr><th>初始堆大小</th><td>" + (init >> 10) + "KB" + "</td></tr>");
        System.out.println("<tr><th>最大堆大小</th><td>" + (max >> 10) + "KB" + "</td></tr>");
        System.out.println("<tr><th>已分配大小</th><td>" + (committed >> 10) + "KB" + "</td></tr>");
        System.out.println("<tr><th>已使用堆大小</th><td>" + (used >> 10) + "KB" + "</td></tr>");

        MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage();
        long nonHeapInit = nonHeapMemoryUsage.getInit();
        long nonHeapUsed = nonHeapMemoryUsage.getUsed();
        long nonHeapCommitted = nonHeapMemoryUsage.getCommitted();
        long nonHeapMax = nonHeapMemoryUsage.getMax();
        System.out.println("<tr><th>初始非堆大小(分配大小)</th><td>" + (nonHeapInit >> 10) + "KB" + "</td></tr>");
        System.out.println("<tr><th>最大非堆大小</th><td>" + (nonHeapMax >> 10) + "KB" + "</td></tr>");
        System.out.println("<tr><th>已分配非堆大小</th><td>" + (nonHeapCommitted >> 10) + "KB" + "</td></tr>");
        System.out.println("<tr><th>已使用非堆大小(当前非堆大小)</th><td>" + (nonHeapUsed >> 10) + "KB" + "</td></tr>");

        List<GarbageCollectorMXBean> garbageCollectorMXBeans = ManagementFactory.getGarbageCollectorMXBeans();
        for (GarbageCollectorMXBean garbageCollectorMXBean : garbageCollectorMXBeans) {
            System.out.println("<tr><th>垃圾收集器</th><td>Name=" + garbageCollectorMXBean.getName() + ", Collections=" + garbageCollectorMXBean.getCollectionCount() + ", Total time spent=" + garbageCollectorMXBean.getCollectionTime() + "</td></tr>");
        }

        // ----------------------------
        OperatingSystemMXBean osMXBean = ManagementFactory.getOperatingSystemMXBean();
        String osName = osMXBean.getName();
        String version = osMXBean.getVersion();
        String osArch = osMXBean.getArch();
        int processors = osMXBean.getAvailableProcessors();

        System.out.println("<tr><th>操作系统</th><td>" + osName + "(" + version + ")" + "</td></tr>");
        System.out.println("<tr><th>架构</th><td>" + osArch + "</td></tr>");
        System.out.println("<tr><th>处理器数量</th><td>" + processors + "</td></tr>");

        com.sun.management.OperatingSystemMXBean osx = (com.sun.management.OperatingSystemMXBean) osMXBean;
        long totalPhysicalMemorySize = osx.getTotalPhysicalMemorySize();
        long freePhysicalMemorySize = osx.getFreePhysicalMemorySize();
        long totalSwapSpaceSize = osx.getTotalSwapSpaceSize();
        long freeSwapSpaceSize = osx.getFreeSwapSpaceSize();
        long committedVirtualMemorySize = osx.getCommittedVirtualMemorySize();
        System.out.println("<tr><th>分配虚拟内存</th><td>" + committedVirtualMemorySize + "</td></tr>");
        System.out.println("<tr><th>物理内存大小</th><td>" + (totalPhysicalMemorySize >> 10 >> 10 >> 10) + "GB" + "</td></tr>");
        System.out.println("<tr><th>可用物理内存</th><td>" + (freePhysicalMemorySize >> 10 >> 10 ) + "MB" + "</td></tr>");
        System.out.println("<tr><th>交换空间总量</th><td>" + totalSwapSpaceSize + "</td></tr>");
        System.out.println("<tr><th>可用交换空间</th><td>" + freeSwapSpaceSize + "</td></tr>");

        List<String> inputArguments = runtimeMXBean.getInputArguments();
        String classPath = runtimeMXBean.getClassPath();
        String libraryPath = runtimeMXBean.getLibraryPath();
        String bootClassPath = runtimeMXBean.getBootClassPath();
        System.out.println("<tr><th>VM 参数</th><td>" + inputArguments + "</td></tr>");
        System.out.println("<tr><th>ClassPath</th><td>" + classPath + "</td></tr>");
        System.out.println("<tr><th>LibraryPath</th><td>" + libraryPath + "</td></tr>");
        System.out.println("<tr><th>BootClassPath</th><td>" + bootClassPath + "</td></tr>");
        System.out.println("<tr><th>Current User</th><td>" + System.getProperty("user.name") + "</td></tr>");
        System.out.println("<tr><th>OS TimeZone</th><td>" + TimeZone.getDefault().getID() + "</td></tr>");


        System.out.println(System.console());
        ArrayUtil.createArray();
        // Sequence
        // ArrayUtils.remove()
            // System.console().format("\033[31m呵呵\033[0m");

        // merge sequence, map
        // BeanMap

    }

    public static <E> E[] create(Class<E> type) {
        return null;
    }
}
